package com.maiworld.seller.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.maiworld.mapper.TbCoinMapper;
import com.maiworld.mapper.TbStudentMapper;
import com.maiworld.pojo.TbCoin;
import com.maiworld.pojo.TbStudent;
import com.maiworld.seller.service.CoinService;

@Service
public class CoinServiceImpl implements CoinService {

	@Autowired
	private TbCoinMapper coinMapper;
	
	@Autowired
	private TbStudentMapper tbStudentMapper;

	//用户的金币详情 id:用户的id
	@Override
	public Map<String, Object> findAll(String openid, Long sid){
		Map<String, Object> result = new HashMap<String, Object>();

		List<TbCoin> recorde = new ArrayList<>();
		recorde = coinMapper.selectByStudentID(sid);
		List<TbCoin> recorde2 = new ArrayList<>(); 
		List<TbCoin> consume = new ArrayList<>();
		
		for(TbCoin coin :recorde){
			TbCoin co = new TbCoin();
			if(coin.getState().equals("1")){
				co.setId(coin.getId());
				co.setSid(coin.getSid());
				co.setComment(coin.getComment());
				co.setTime(coin.getTime());
				co.setState(coin.getState());
				co.setPoint(coin.getPoint());
				recorde2.add(co);				
			}
			if(coin.getState().equals("0")){
				co.setId(coin.getId());
				co.setSid(coin.getSid());
				co.setComment1(coin.getComment1());
				co.setState(coin.getState());
				co.setTime1(coin.getTime1());
				co.setPoint1(coin.getPoint1());
				consume.add(co);
			}			
		}		
		String a = "金币是：一种虚拟币，但是积分记录将按实际使用情况减少";
		
		TbStudent array = new TbStudent();
		array = tbStudentMapper.selectByPrimaryKey(sid);
		int points = array.getPoints();
		
		Map<String, Object> recordeRes = new HashMap<String, Object>();
		recordeRes.put("number", points);
		recordeRes.put("details", a);
		
		if(recorde.isEmpty()){
			recordeRes.put("recorde", null);
		}else {
			recordeRes.put("recorde", recorde2);
			recordeRes.put("consume", consume);
		}
		result.put("recorde", recordeRes);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
		
	}

	//获得/消费--金币详情(提交数据)
	@Override
	public Map<String, Object> add(Map<String, String> parmes) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		TbCoin coin = new TbCoin();
	
		if((parmes.get("state")).equals("0")){
			coin.setComment1(parmes.get("comment1"));
			coin.setPoint1(parmes.get("point1"));
			coin.setSid(Long.parseLong(parmes.get("sid")));
			coin.setTime1(new Date());
			coin.setState(parmes.get("state"));
			coinMapper.insert(coin);
			result.put("statusCode", 200);
			result.put("message", "添加成功");		
		}else {
			
			coin.setComment(parmes.get("comment"));
			coin.setPoint(parmes.get("point"));
			coin.setSid(Long.parseLong(parmes.get("sid")));
			coin.setTime(new Date());
			coin.setState(parmes.get("state"));
			coinMapper.insert(coin);
			result.put("statusCode", 200);
			result.put("message", "添加成功");		
		}
		return result;
	}


}
